package com.program.services;

import com.program.controller.MainController;
import com.program.eneity.Airplane;
import com.program.eneity.Date;

/**
 * @program: AirportSystem
 * @description: 事件服务
 * @author: 19071027
 * @create: 2021-09-10 21:13
 **/
public class EventService {
    /**
     * 虚拟单位时间数(分)
     */
    private int VIRTUAL_TIME = 5;

    /**
     * 虚拟单位时间对应的实际时间(毫秒)
     */
    private int REAL_TIME = 2000;

    AirplaneService airplaneService = AirplaneService.getAirplaneService();

    public int getVirtualTime() {
        return VIRTUAL_TIME;
    }

    public void setVirtualTime(int VIRTUAL_TIME) {
        this.VIRTUAL_TIME = VIRTUAL_TIME;
    }

    public int getRealTime() {
        return REAL_TIME;
    }

    public void setRealTime(int REAL_TIME) {
        this.REAL_TIME = REAL_TIME;
    }

    /**
     * 减少燃油
     */
    public void reduceFuel() {
        Airplane[] airplanes = airplaneService.getAirplanes();
        for (int i = 0; i < airplaneService.getNum(); i++) {
            if (airplanes[i] != null && airplaneService.isLandingTime(MainController.date, airplanes[i])) {
                airplanes[i].setFuel(airplanes[i].getFuel() - 5);
            }
        }
    }

    /**
     * 改变虚拟时间
     * @param date 当前时间
     * @return 新的时间
     */
    public Date changeDate(Date date) {
        int newMin = (Integer.valueOf(date.getMinute()) + VIRTUAL_TIME) % 60;
        int newHour = (Integer.valueOf(date.getHour()) + ((Integer.valueOf(date.getMinute()) + VIRTUAL_TIME) / 60)) % 24;
        if (newHour < 10) {
            date.setHour("0" + newHour);
        }
        else {
            date.setHour(String.valueOf(newHour));
        }

        if (newMin < 10) {
            date.setMinute("0" + newMin);
        }
        else {
            date.setMinute(String.valueOf(newMin));
        }

        return date;
    }

}
